<template>
  <div class="container">
    <div class="container__box">
      <div class="container__box__img" @click="dialogVisible = !dialogVisible">
        <img src="http://www.dell-lee.com/imgs/vue3/basket.png" alt="" />
      </div>
      <div class="container__box__num" v-if="cartList.length !== 0">
        {{ caculations.total }}
      </div>
    </div>

    <div class="container__infobox">
      <span>总计：</span>
      <span class="container__infobox__info">{{ caculations.price }}</span>
    </div>
    
    <div class="container__button" v-show="caculations.total > 0"><!--商品总数大于0的时候才显示-->
      <router-link :to="`/comfirmOrder/${shopId}`"> 去结算 </router-link>
    </div>
  </div>
  <div class="dialog" v-show="caculations.total > 0 && dialogVisible">
    <div class="bgc" @click="dialogVisible = false"></div>
    <div class="product">
      <div class="clearall">
        <div
          class="clearall__icon iconfont"
          @click="() => setCartItemsChecked(shopId, caculations.allChecked)"
          v-html="caculations.allChecked ? '&#xe66c;' : '&#xe7ae;'"
        ></div>
        <div class="clearall__desc" @click="() => clearCarData(shopId)">清空购物车</div>
      </div>
      <template v-for="item in productList">
        <!-- <div class="item" v-if="cartList[shopId][item._id].count"> -->
        <!-- 加了notEmptyList中添加了sumPrice后 没有count所以会报错-->
        <div class="item" v-if="(typeof item) !== 'number'">
          <span
            @click="() => changeCartItemChecked(shopId, item._id)"
            v-html="item.check ? '&#xe66c;' : '&#xe7ae;'"
            style="font-size: 20px; line-height: 65px"
            class="iconfont"
          >
          </span>
          <div style="margin-right: 16px">
            <img class="item__img" :src="item.imgUrl" alt="" />
          </div>
          <div style="flex: 1" class="item__right">
            <span class="item__title">{{ item.name }}</span>
            <div style="height: 8px"></div>
            <span class="item__price">¥{{ item.price }} </span>
            <span class="item__oldPrice">¥{{ item.oldPrice }}</span>
            <div class="item__plusBox">
              <span
                @click="changeCartItemInfo(shopId, item._id, item, 1)"
                class="iconfont item__icon"
                >&#xe667;</span
              >
              <span style="flex: 1; text-align: center">
                {{ cartList?.[shopId]?.[item._id]?.count || 0 }}
              </span>
              <span
                @click="changeCartItemInfo(shopId, item._id, item, -1)"
                style="margin-top: 1px"
                class="iconfont item__icon"
                >&#xe66d;</span
              >
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>
<script>
import { ref } from "vue";
import { useStore } from "vuex";
import { useRoute } from "vue-router";
import { toRefs } from "vue";
import { useCommomCartEffect } from "../effect/useCartEffect.js";
import { ElMessageBox } from "element-plus";

const cartEffect = (shopId) => {
  const store = useStore();
  const { changeCartItemInfo, caculations, productList, cartList } = useCommomCartEffect(
    shopId
  );
  // 改变商品选中状态
  const changeCartItemChecked = (shopId, productId) => {
    store.commit("changeCartItemChecked", { shopId, productId });
  };
  // 全选商品
  const setCartItemsChecked = (shopId, allChecked) => {
    store.commit("setCartItemsChecked", { shopId, allChecked });
  };
  const clearCarData = (shopId) => {
    store.commit("clearCarData", { shopId });
  };
  return {
    changeCartItemInfo,
    caculations,
    productList,
    cartList,
    changeCartItemChecked,
    setCartItemsChecked,
    clearCarData,
  };
};

export default {
  setup() {
    const dialogVisible = ref(false);
    const route = useRoute();
    const shopId = route.params.id;
    const {vuexTotalPrice} = useStore().state;
    const {
      changeCartItemInfo,
      caculations,
      productList,
      cartList,
      changeCartItemChecked,
      setCartItemsChecked,
      clearCarData,
    } = cartEffect(shopId);
    console.log("cartproductList", productList);
    return {
      vuexTotalPrice,
      changeCartItemInfo,
      shopId,
      caculations,
      productList,
      cartList,
      dialogVisible,
      changeCartItemChecked,
      setCartItemsChecked,
      clearCarData,
    };
  },
};
</script>
<style lang="scss" scoped>
.container {
  height: 49px;
  position: fixed;
  width: 100%;
  left: 0px;
  // bottom:13px;
  bottom: 0px;
  background-color: #fff;
  display: flex;

  &__box {
    position: relative;
    padding-left: 30px;
    box-sizing: border-box;
    padding-top: 5px;
    flex: 0.5;

    img {
      width: 28px;
      height: 28px;
    }

    &__num {
      position: absolute;
      left: 50px;
      top: 0px;
      width: 16px;
      height: 16px;
      background-color: red;
      color: #fff;
      border-radius: 50%;
      font-size: 12px;
      text-align: center;
      line-height: 16px;
    }
  }

  &__infobox {
    flex: 1;
    line-height: 49px;
    font-size: 14px;
    &__info {
      font-size: 18px;
      color: #e93b3b;
      font-weight: bold;
    }
  }

  &__button {
    flex: 1;
    width: 98px;
    height: 49px;
    background-color: #4fb0f9;
    color: #fff;
    text-align: center;
    line-height: 49px;
  }
}

.dialog {
  position: fixed;
  bottom: 50px;
  left: 0px;
  right: 0px;
  top: 0px;
  //   background: rgba(0, 0, 0, 0.5);
  .bgc {
    position: absolute;
    bottom: 0px;
    left: 0px;
    right: 0px;
    top: 0px;
    background: rgba(0, 0, 0, 0.5);
  }
  .product {
    background-color: #fff;
    position: absolute;
    bottom: 0px;
    left: 0px;
    right: 0px;
    .clearall {
      border-bottom: 1px solid #f1f1f1;
      display: flex;
      line-height: 52px;
      padding-left: 18px;
      padding-right: 18px;
      &__icon {
        flex: 1;
        font-size: 20px;
      }
      &__desc {
        flex: 1;
        text-align: right;
      }
    }
    .item {
      margin-left: 20px;
      margin-top: 10px;
      display: flex;
      &__img {
        margin-left: 15px;
      }
      img {
        width: 56px;
        height: 56px;
      }

      &__plusBox {
        width: 60px;
        float: right;
        margin-right: 10px;
        display: flex;
      }

      &__right {
        border-bottom: 1px solid #f1f1f1;
        padding-bottom: 12px;
      }

      &__title {
        font-size: 14px;
        font-weight: bold;
        color: #333;
        line-height: 22px;
      }

      &__desc1 {
        font-size: 13px;
        color: #333;
        line-height: 18px;
        margin-right: 16px;
      }

      &__price {
        color: #e93b3b;
      }

      &__oldPrice {
        color: #999;
        font-size: 10px;
        text-decoration: line-through;
      }

      &__icon {
        color: #0091ff;
        font-size: 19px;
        flex: 1;
      }

      &__discountinfo {
        color: #e93b3b;
        font-size: 13px;
        line-height: 18px;
      }
    }
  }
}
</style>
